package rs.fon.is.music.parser;

import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import rs.fon.is.music.domain.MusicGroup;
import rs.fon.is.music.services.MusicGroupService;

public class LastFMParser {
	
	MusicGroupParser groupParser;
	
	public LastFMParser() {
		groupParser = new MusicGroupParser();
	}

	public LinkedList<MusicGroup> parse(String url) throws Exception {

		Document doc;
		
		try {
			doc = Jsoup.connect(url).get();
			
			Elements tagListElements = doc.getElementsByClass("tagList");
			
			Elements tagElements = tagListElements.first().getElementsByTag("li");
					
			LinkedList<MusicGroup> groups = new LinkedList<>();
			
			String noHttp = url.substring(7, url.length());
			String domain = noHttp.substring(0, noHttp.indexOf('/'));
			
			for (Element tagElement : tagElements) {
				
				if(!tagElement.text().equals("All")){
					
					String categoryURL = tagElement.getElementsByTag("a").first().attr("href");

					if (!categoryURL.startsWith("http://" + domain))
						categoryURL = "http://" + domain + categoryURL;
					
					groups.addAll(parseCategory(categoryURL));
					
				}
			}
			
			return groups;
		} catch (IOException e) {
			return parse(url);
		}
	}

	public LinkedList<MusicGroup> parseCategory(String url){

		Document doc;
		
		try {
			doc = Jsoup.connect(url).get();
			
			Elements paginationElements = doc.getElementsByClass("pagination");
			
			int pages = 1;
			
			if(paginationElements.size()>0){
				pages = Integer.parseInt(paginationElements.first().getElementsByClass("lastpage").first().text());
			}
			
			LinkedList<MusicGroup> groups = new LinkedList<>();
			
			MusicGroupService service = new MusicGroupService();
			
			// UMESTO 1 IDE PAGES
			for (int i = 0; i < 1; i++) {
				
				try {
					if(i>0){
						int newPage = i+1;
						doc =  Jsoup.connect(url + "?page=" + newPage).get();
					}
					
					Elements artistListElements = doc.getElementsByClass("artistList");

					if (artistListElements.size() > 0) {

						Element artistListElement = artistListElements.first();
						Elements artistElements = artistListElement.getElementsByTag("li");

						String noHttp = url.substring(7, url.length());
						String domain = noHttp.substring(0, noHttp.indexOf('/'));

						for (Element artistElement : artistElements) {

							String groupURL = artistElement.getElementsByTag("a").first().attr("href");

							if (!groupURL.startsWith("http://" + domain))
								groupURL = "http://" + domain + groupURL;
							
							if(!service.doesMusicGroupExist(new URL(groupURL)))
								groups.add(groupParser.parse(groupURL));

						}
					}
				
				} catch (IOException e) {
					i--; //?
				} catch (Exception e) {
					
				}
			}

			return groups;
		
		} catch (IOException e) {
			return parseCategory(url);
		}		
	}
}
